package io.finer.erp.jeecg.bas.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.finer.erp.jeecg.bas.entity.BasTax;
import io.finer.erp.jeecg.bas.service.BasTaxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * <p>
 * 税率 前端控制器
 * </p>
 *
 * @author yaoyifan
 * @since 2021-06-21
 */
@Api(tags = "税率")
@RestController
@RequestMapping("/bas/basTax")
@Slf4j
public class BasTaxController {
    @Autowired
    private BasTaxService basTaxService;

    /**
     * 分页列表查询
     *
     * @param basTax
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "税率-分页列表查询")
    @ApiOperation(value="税率-分页列表查询", notes="税率-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(BasTax basTax,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<BasTax> queryWrapper = QueryGenerator.initQueryWrapper(basTax, req.getParameterMap());
        Page<BasTax> page = new Page<BasTax>(pageNo, pageSize);
        IPage<BasTax> pageList = basTaxService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     *   添加
     *
     * @param basTax
     * @return
     */
    @AutoLog(value = "税率-添加")
    @ApiOperation(value="税率-添加", notes="税率-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody BasTax basTax) {
        basTaxService.save(basTax);
        return Result.ok("添加成功！");
    }

    /**
     *  编辑
     *
     * @param basTax
     * @return
     */
    @AutoLog(value = "税率-编辑")
    @ApiOperation(value="税率-编辑", notes="税率-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody BasTax basTax) {
        basTaxService.updateById(basTax);
        return Result.ok("编辑成功!");
    }

    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "税率-通过id删除")
    @ApiOperation(value="税率-通过id删除", notes="税率-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        basTaxService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     *  批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "税率-批量删除")
    @ApiOperation(value="税率-批量删除", notes="税率-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.basTaxService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "税率-通过id查询")
    @ApiOperation(value="税率-通过id查询", notes="税率-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        BasTax basTax = basTaxService.getById(id);
        if(basTax==null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(basTax);
    }
}

